

package rosedb.gui;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import riulib.gui.imagelist.AbstractImageList;

import riulib.gui.imagelist.ReferenzImageList;
import riulib.helpers.ScaleImage;
import rosedb.control.Controller;
import rosedb.model.Logger;
import rosedb.model.Rose;


/**
 *
 * @author riu
 */
public class RosesImageList extends ReferenzImageList<Rose>{

    private BufferedImage defaultImg;
    private Controller control;

    public RosesImageList() {
        super(Alignment.Horizontal, false);
        
        try{
            defaultImg = ImageIO.read(this.getClass().getResourceAsStream("/rosedb/resources/nopics.jpg"));
        }catch(Exception e){
            Logger.log(e);
        }

        setVertikalTitlePosition(AbstractImageList.VertikalTitlePosition.AtBottom);
        setForceSquaredImages(false);
        setMargin(0, 0, 0, 0);
        setPadding(0, 0, 0, getFont().getSize());
    }

    public void init(Controller c){
        control = c;
    }

    public void addRose(Rose r){
        try{
            if(r!=null && !containsReferenz(r)){
                File f = new File(Controller.DATA_DIR+"roses/"+r.getName().toLowerCase()+"/img0.jpg");
                BufferedImage img = defaultImg;
                if(f.exists()){
                    img = ImageIO.read(f);    
                }
                
                ScaleImage.Rectangle preferedSize = ScaleImage.fitToRect(new ScaleImage.Rectangle(0, 0, fotoWidth, fotoHeigth), img);
                addImage(ScaleImage.scale(img, preferedSize.width, preferedSize.heigth), r.getName(), r);
                sort(control.getCurrentComperator(), true, Controller.SORT_SPEED, true, ReferenzImageList.SortAlgorithm.Quicksort);
            }
        }catch(Exception e){
            Logger.log(e);
        }
        
    }

    public void reloadRose(int index){
        if(index>=0 && index < referenzes.size()){
            Rose r = getReferenz(index);
            
            BufferedImage img = null;
            try{
                File f = new File(Controller.DATA_DIR+"roses/"+r.getName().toLowerCase()+"/img0.jpg");
                if(f.exists()){
                    img = ImageIO.read(f);
                }
                else{
                    img = defaultImg;
                }
                
                ScaleImage.Rectangle preferedSize = ScaleImage.fitToRect(new ScaleImage.Rectangle(0, 0, fotoWidth, fotoHeigth), img);
                replaceImage(index, ScaleImage.scale(img, preferedSize.width, preferedSize.heigth));
            }catch(Exception e){
                Logger.log(e);
            }
        }
    }

    public void reloadRose(Rose r){
       reloadRose(indexOf(r));
    }

    @Override
    public synchronized void paint(Graphics g) {
        g.setColor(getParent().getBackground());
        g.fillRect(0, 0, getWidth(), getHeight());

        Graphics2D g2d = (Graphics2D)g.create();
        RoundRectangle2D r = new RoundRectangle2D.Float(0, 0, this.getWidth(), this.getHeight(), 20, 20);
        g2d.setClip(r);
        super.paint(g2d);
    }

    


}
